﻿using Godot;
using Godot.Collections;
using System;
using System.Collections.Generic;
using Array = Godot.Collections.Array;

public class Recipes
{
    public Recipes()
    {
        Load();
    }
    public static List<Recipe> recipes = new List<Recipe>();
    public static Recipe RegiseryRecipe(Recipe recipe)
    {
        recipe.id = recipes.Count;
        recipes.Add(recipe);
        return recipe;
    }

    public void Load()
    {
        List<String> files = new List<string>();
        Directory dir = new Directory();
        if (dir.Open("Config/Recipe") != Error.Ok)
        {
            dir.MakeDirRecursive("Config/Recipe");
        }
        dir.ListDirBegin(true);
        String filename = dir.GetNext();
        while (filename != "")
        {
            if (!dir.CurrentIsDir())
            {
                if (filename.Contains(".json"))
                    files.Add(filename);
            }
            filename = dir.GetNext();
        }
        dir.ListDirEnd();
        //

        foreach (String fn in files)
        {
            GD.Print("加载配方:" + fn);
            File f = new File();
            if (f.Open("Config/Recipe/" + fn, File.ModeFlags.Read) != Error.Ok)
            {

            }

            List<Dictionary> dicts = new List<Dictionary>();
            object result = JSON.Parse(f.GetAsText()).Result;
            f.Close();
            if (result is Array)
            {
                Array array = (Array)result;
                for (int i = 0; i < array.Count; i++)
                {
                    dicts.Add((Dictionary)array[i]);
                }
            }
            if (result is Dictionary)
            {
                dicts.Add((Dictionary)result);
            }

            for (int index = 0; index < dicts.Count; index++)
            {

                Dictionary dict = dicts[index];
                //GD.Print(JSON.Print(dict));
                Recipe recipe = new Recipe();
                String blockname = dict["block"] as string;
                if (blockname != "null")
                {
                    recipe.block = Materials.GetBlockMaterial(blockname);
                }
                else
                {
                    recipe.block = null;
                }
                Array target = (Array)dict["target"];
                Array res = (Array)dict["res"];
                for (int i = 0; i < target.Count; i++)
                {
                    Dictionary td = (Dictionary)target[i];
                    ItemStack item = Materials.GetItemMateria(td["name"] as string).GetItemStack(td["amount"].ToInt());
                    recipe.AddTarget(item);
                }
                for (int i = 0; i < res.Count; i++)
                {
                    Dictionary td = (Dictionary)res[i];
                    ItemStack item = Materials.GetItemMateria(td["name"] as string).GetItemStack(td["amount"].ToInt());
                    recipe.AddRes(item);
                }
                RegiseryRecipe(recipe);
            }
        }
    }
}

